import Vue from "vue";
import Router from "vue-router";
import ROUTERS from "./routers";
Vue.use(Router);

let router = new Router({
  routes: ROUTERS,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition;
    } else {
      return {
        x: 0,
        y: 0,
      };
    }
  },
});
router.beforeEach((to, from, next) => {
  if (to.name == "Login") {
    next();
  } else {
    if (localStorage.getItem("token")) {
      next();
    } else {
      next("/Login");
    }
  }
});
const originalPush = Router.prototype.push;
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};

export default router;
